package com.quectel.cms.controller.sys;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.sys.module.sys.dto.SysUserDto;
import com.quectel.sys.module.sys.service.SysGroupService;
import com.quectel.sys.module.sys.service.SysUserService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 组用户管理
 *
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-04-01 13:06:24
 */
@RestController
@RequestMapping("sysGroupUser")
@Tag(name = "SysGroupUserController", description = "系统组用户相关api")
public class SysGroupUserController extends BaseController {

    @DubboReference
    private SysUserService sysUserService;
    @DubboReference
    private SysGroupService sysGroupService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "sysGroupUser:list")
    @Operation(summary = "系统组用户分页列表查询")
    public Response<Page<SysUserDto>> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Request request = Request.configParams(params);
        String groupId = request.getString("groupId");
        if (StringUtils.isBlank(groupId)) {
            return Response.error("组id不能为空");
        }

        List<SysUserDto> list = sysUserService.queryList(params);
        int total = sysUserService.queryTotal(params);
        if (list != null) {
            for (SysUserDto sysUserDto : list) {
                if (sysUserDto.getGroupId() != null) {
                    sysUserDto.setSysGroup(sysGroupService.selectById(sysUserDto.getGroupId()));
                }
            }
        }

        Page<SysUserDto> page = new Page(Integer.valueOf(params.get("pageNo") + ""), Integer.valueOf(params.get("pageSize") + ""), total, list);

        return Response.<Page<SysUserDto>>ok().wrap(page);
    }


    /**
     * 根据ID查询系统组用户
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "sysGroupUser:info")
    @Operation(summary = "根据ID查询系统组用户记录")
    public Response<SysUserDto> info(@PathVariable("id") Long id) {
        SysUserDto sysUserDto = sysUserService.selectById(id);

        return Response.<SysUserDto>ok().wrap(sysUserDto);
    }

    /**
     * 保存系统组用户记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "sysGroupUser:edit")
    @Operation(summary = "保存系统组用户记录")
    public Response<Object> save(@RequestBody @Validated SysUserDto sysUserDto) {
        SysUserDto dbUser = sysUserService.selectByAccount(sysUserDto.getAccount());
        if (dbUser != null) {
            return Response.error("该账号已存在");
        }
        sysUserService.save(sysUserDto);
        return Response.ok();
    }

    /**
     * 修改系统组用户记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "sysGroupUser:edit")
    @Operation(summary = "修改系统组用户记录")
    public Response<Object> update(@RequestBody @Validated SysUserDto sysUserDto) {
        SysUserDto dbUser = sysUserService.selectByAccount(sysUserDto.getAccount());
        if (dbUser != null && !Objects.equals(sysUserDto.getId(), dbUser.getId())) {
            return Response.error("您修改的账号已存在");
        }
        sysUserService.updateById(sysUserDto);
        return Response.ok();
    }

    /**
     * 重置系统组用户密码
     */
    @PostMapping("resetPwd")
    @RequirePermissions(values = "sysGroupUser:edit")
    @Operation(summary = "重置系统组用户密码")
    public Response<Object> resetPwd(@RequestBody Long[] ids) {
        for (Long id : ids) {
            sysUserService.updatePassword(id, "123456");
        }
        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "sysGroupUser:delete")
    @Operation(summary = "批量删除系统组用户")
    public Response<Object> delete(@RequestBody Long[] ids) {
        for (Long id : ids) {
            SysUserDto sysUserDto = sysUserService.selectById(id);
            if ("admin".equalsIgnoreCase(sysUserDto.getAccount())) {
                return Response.error("超级管理员不能被删除");
            }
        }
        sysUserService.deleteBatch(ids);
        return Response.ok();
    }
}
